package com.groupbuying.sysconfig.controller;

import java.util.List;
import java.util.Map;

import com.groupbuying.sysconfig.constants.CommonConstants;
import com.groupbuying.sysconfig.constants.RedisCacheConstants;
import com.groupbuying.sysconfig.constants.SysconfigOpexRuleSearchConstants;
import com.groupbuying.sysconfig.dto.do2dto.SysconfigOpexRuleConvert;
import com.groupbuying.sysconfig.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.groupbuying.sysconfig.domain.SysconfigOpexRuleDO;
import com.groupbuying.sysconfig.service.SysconfigOpexRuleService;

/**
 * 运营规则配置表
 * 
 * @author ywxd
 * @email yuwenxiaodong@126.com
 * @date 2019-05-31 01:19:21
 */
 
@RestController
@RequestMapping("/opexRule")
public class SysconfigOpexRuleController {
	private static final Logger logger = LoggerFactory.getLogger(SysconfigOpexRuleController.class);

	@Autowired
	private SysconfigOpexRuleService sysconfigOpexRuleService;

	@Autowired
	private RedisUtils redisUtils;

	/* 根据传入字段列出规则列表 */
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		String redisKey_content = RedisCacheConstants.CACHE_KEY_RULE_CONTENT + query.getPage() + query.getOffset() + query.getLimit();
		String redisKey_count = RedisCacheConstants.CACHE_KEY_RULE_COUNT + query.getPage() + query.getOffset() + query.getLimit();

		if(redisUtils.isHasKey(redisKey_content) && redisUtils.isHasKey(redisKey_count)){
			//命中缓存
			logger.info("命中缓存，key:" + redisKey_content);
			int total = (int)redisUtils.getValue(redisKey_count);
			List sysconfigOpexRuleList = redisUtils.lGetAll(redisKey_content);
			List resultList = SysconfigOpexRuleConvert.MAPPER.dos2dtos(sysconfigOpexRuleList);
			PageUtils pageUtils = new PageUtils(resultList, total);
			return pageUtils;
		}

		//新建缓存
		List sysconfigOpexRuleList = sysconfigOpexRuleService.list(query);
		List resultList = SysconfigOpexRuleConvert.MAPPER.dos2dtos(sysconfigOpexRuleList);
		int total = sysconfigOpexRuleService.count(query);
		redisUtils.setList(redisKey_content,sysconfigOpexRuleList);
		redisUtils.setKey(redisKey_count,total);
		PageUtils pageUtils = new PageUtils(resultList, total);
		return pageUtils;
	}

	/**
	* 根据ruleID获取规则
	* key-ruleId
	* */

	@GetMapping("/getRule")
	public Map getRule(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		//缓存处理
		String redisKey = RedisCacheConstants.CACHE_KEY_RULE_CONTENT + params.get("ruleId");
		if(redisUtils.isHasKey(redisKey)){
			//命中
			logger.info("命中缓存，key:" + redisKey);
			return ObjectUtils.objectToMap(redisUtils.getValue(redisKey));
		}
		//新建
		params.put(SysconfigOpexRuleSearchConstants.SEARCH_DELFLAG,CommonConstants.COMMON_FLAG_UNDEL);
		List<SysconfigOpexRuleDO> sysconfigOpexRuleList = sysconfigOpexRuleService.list(query);
		SysconfigOpexRuleDO sysconfigOpexRuleDO = sysconfigOpexRuleList.get(0);
		redisUtils.setKey(redisKey,sysconfigOpexRuleDO);
		return ObjectUtils.objectToMap(sysconfigOpexRuleDO);
	}

	/**
	 * Description 创建一条运营规则，需要参数有：
	 *     1）线上非积分消费返积分率：backCurrencyRatio 必填；
	 *     2）积分消费打折率：currencyPayDiscount 必填；
	 *     3）运营规则描述：ruleDes 选填
	 *
	 * @author Ywxd <br>
	 * @param  sysconfigOpexRule
	 * @return R <br>
	 * @createDate 2019/4/17 9:17<br>
	 **/
	@PostMapping("/create")
	public R save(@RequestBody Map<String, Object> params){
		return sysconfigOpexRuleService.createOpexRule(params);
	}

	/**
	 * 修改运营规则
	 */
	@RequestMapping("/update")
	public R update( SysconfigOpexRuleDO sysconfigOpexRule){
		sysconfigOpexRuleService.update(sysconfigOpexRule);

		//清除缓存
		redisUtils.delKeyCache(RedisCacheConstants.CACHE_KEY_RULE_CONTENT);
		redisUtils.delKeyCache(RedisCacheConstants.CACHE_KEY_RULE_COUNT);
		return R.ok();
	}
	
	/**
	 * 根据ruleId删除运营规则
	 */
	@PostMapping( "/remove")
	public R remove( String ruleId){
		if(sysconfigOpexRuleService.remove(ruleId)>0){
			//清除缓存
			redisUtils.delKeyCache(RedisCacheConstants.CACHE_KEY_RULE_COUNT);
			redisUtils.delKeyCache(RedisCacheConstants.CACHE_KEY_RULE_CONTENT);
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 根据ids批量删除运营规则
	 */
	@PostMapping( "/batchRemove")
	public R remove(@RequestParam("ids[]") String[] ruleIds){
		sysconfigOpexRuleService.batchRemove(ruleIds);
		//清除缓存
		redisUtils.delKeyCache(RedisCacheConstants.CACHE_KEY_RULE_COUNT);
		redisUtils.delKeyCache(RedisCacheConstants.CACHE_KEY_RULE_CONTENT);
		return R.ok();
	}
	
}
